package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.SeleciUserAuthRes;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.ui.adapter.bean.AppHomeCardBean;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/24 18:39
 * description:
 */
public class UserListAdapter extends DelegateAdapter.Adapter<UserListAdapter.UserListHolder> {

    private Context mContext;
    private LayoutHelper mGridLayoutHelper;
    private List<SeleciUserAuthRes.AuthTypeListBean> viewData;


    private BaseInterface baseInterface;
    private AppHomeCardBean lvdata;

    public void setLvdata(AppHomeCardBean lvdata) {
        this.lvdata = lvdata;
    }

    public interface BaseInterface {
        public void onClick(View view, String type);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }


//    private String levelData;

//    public void setLevelData(String levelData) {
//        this.levelData = levelData;
//    }

    public interface MoreInterface {
        public void onClick(View view, int position, String type);
    }

    public void moreOnClick(MoreInterface moreInterface) {
        this.moreInterface = moreInterface;
    }

    private MoreInterface moreInterface;

    public UserListAdapter(FragmentActivity activity, DefaultLayoutHelper defaultLayoutHelper) {
        mContext = activity;
        mGridLayoutHelper = defaultLayoutHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public UserListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.user_list_view, parent, false);

        return new UserListHolder(view);
    }

    public void toLogin() {
        JumpUtils.jump(mContext, "login", 0);
    }

    @Override
    public void onBindViewHolder(final UserListHolder holder, final int position) {


        if (viewData != null) {
            //社保
            final SeleciUserAuthRes.AuthTypeListBean bean1 = viewData.get(0);
            //公积金
            SeleciUserAuthRes.AuthTypeListBean bean2 = viewData.get(1);
            //桂花分
            final SeleciUserAuthRes.AuthTypeListBean bean3 = viewData.get(2);

            final String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");


//,authType:1公积金，2社保，3桂花分；isAuth：0未授权，1已授权，2未实名(L3认证)

            //L3 未认证     、   未授权   、已授权
            if (bean2.getIsAuth().equals("0")) {
                holder.gjjTv.setText("未授权");
            } else if (bean2.getIsAuth().equals("1")) {
                holder.gjjTv.setText("已授权");
            } else {
                holder.gjjTv.setText("L3未认证");
            }

            if (bean3.getIsAuth().equals("0")) {
                holder.ghfTv.setText("未授权");
            } else if (bean3.getIsAuth().equals("1")) {
                holder.ghfTv.setText("已授权");
            } else {
                holder.ghfTv.setText("L3未认证");
            }

            holder.gjjRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //埋点  35 公积金
                    MessageEvent message = new MessageEvent(MessageEvent.CODE_USERBEHAVIOR);
                    message.setData(35);
                    EventBus.getDefault().post(message);
                    Bundle bundle = new Bundle();
                    if ("L3".equals(lvdata.getLevel())) {
                        String path = Constant.BASE_URL + "app-front-end/#/fund/main?access_token=" + access_token;
                        bundle.putString("url", path);
                        bundle.putString(Constant.CUST_NAME, Constant.USER_NAME);
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    } else {
                        bundle.putInt("intotype", 1);
                        bundle.putString("choosetype", "gjj");
                        JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                    }
                }
            });


            holder.ghfRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //桂花分  埋点  2
                    MessageEvent message = new MessageEvent(MessageEvent.CODE_USERBEHAVIOR);
                    message.setData(2);
                    EventBus.getDefault().post(message);
                    //0未授权，1已授权，2未实名(L3认证)
                    if ("L3".equals(lvdata.getLevel())) {
                        //跳转桂花分
                        Bundle bundle = new Bundle();
                        String murl = Constant.SZ_URL + "?" + Constant.EXTENT;
                        bundle.putString("url", murl);
                        bundle.putString(Constant.TITLE, "桂花分");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putInt("intotype", 1);
                        bundle.putString("choosetype", "ghf");
                        JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                    }
                }
            });
        }

        if (lvdata != null) {


            String levelDesc = lvdata.getLevelDesc();
            if (levelDesc.contains("银行卡") && levelDesc.contains("人脸识别") && levelDesc.contains("支付宝")) {
                holder.realLv.setText("L3已认证");
            } else {
                if (levelDesc.indexOf("+") > -1) {
                    holder.realLv.setText(levelDesc.substring(0, levelDesc.length() - 1));
                } else {
                    holder.realLv.setText(levelDesc);
                }

            }

            String level = lvdata.getLevel();
            if (level.equals("L2") || level.equals("L1")) {
                holder.realLv.setText(level + "认证");
            }
        }

        final String access_token = ACache.get(mContext).getAsString("access_token");
        holder.dzglRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toClick(access_token, "adressadmin");
            }
        });
        holder.wdscRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toClick(access_token, "collection");
            }
        });

        holder.cjwtRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (access_token == null) {
                    toLogin();
                } else {
                    String url = Constant.BASE_URL + "app-front-end/#/serviceMain";
                    Bundle bundle = new Bundle();
                    bundle.putString("url", url);
                    JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                }
            }
        });

        holder.gjjRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //埋点  35 公积金
                if (access_token == null) {
                    toLogin();
                }
            }
        });
        holder.wdfpRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (access_token == null) {
                    toLogin();
                } else {
                    //我的发票
                    String url = Constant.BASE_URL + "app-front-end/#/invoice/main?access_token=" + access_token;
                    Bundle bundle = new Bundle();
                    bundle.putString("url", url);
                    JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                    MessageEvent message = new MessageEvent(MessageEvent.CODE_USERBEHAVIOR);
                    message.setData(9);
                    EventBus.getDefault().post(message);

                }
            }
        });

        holder.dzsmkRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (access_token == null) {
                    toLogin();
                } else {
                    String level = lvdata.getLevel();
                    if (level.equals("L1")) {
                        JumpUtils.jump(mContext, "RealNameActivity", 0);
                    } else {
                        Bundle bundle = new Bundle();
                        JumpUtils.gotoNeedGesture(mContext, "elecCitizenCard", bundle);
                    }
                }
            }
        });

        holder.realRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (access_token == null) {
                    toLogin();
                } else {
                    JumpUtils.jump(mContext, "RealNameActivity", 0);
                }
            }
        });


        holder.mCityCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (access_token == null) {
                    toLogin();
                } else {
                    MessageEvent message = new MessageEvent(MessageEvent.CODE_USERBEHAVIOR);
                    message.setData(50);
                    EventBus.getDefault().post(message);
                    JumpUtils.jump(mContext, "MyCityCardActivity", 0);
                }
            }
        });


        holder.wdjkRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (access_token == null) {
                    toLogin();
                } else {
                    JumpUtils.jump(mContext, "DonationActivity", 0);
                }
            }
        });


    }


    private void toClick(String access_token, String url) {
        if (access_token == null) {
            toLogin();
        } else {
            JumpUtils.jump(mContext, url, 0);
        }


    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setViewData(List<SeleciUserAuthRes.AuthTypeListBean> viewData) {
        this.viewData = viewData;
    }


    public static class UserListHolder extends RecyclerView.ViewHolder {


        private final RelativeLayout mCityCard;
        private final RelativeLayout ghfRl;
        private final TextView gjjTv;
        private final TextView ghfTv;
        private final TextView realLv;
        private final RelativeLayout cjwtRl;
        private final RelativeLayout dzglRl;
        private final RelativeLayout wdscRl;
        private final RelativeLayout wdfpRl;
        private final RelativeLayout dzsmkRl;
        private final RelativeLayout realRl;
        private final RelativeLayout wdjkRl;
        private final RelativeLayout gjjRl;

        public UserListHolder(View itemView) {
            super(itemView);
            mCityCard = (RelativeLayout) itemView.findViewById(R.id.userlist_mycitycard_rl);
            ghfRl = (RelativeLayout) itemView.findViewById(R.id.user_ghf_rl);
            gjjTv = (TextView) itemView.findViewById(R.id.user_gjj_tv);
            ghfTv = (TextView) itemView.findViewById(R.id.user_ghf_tv);
            realLv = (TextView) itemView.findViewById(R.id.real_lv);
            cjwtRl = (RelativeLayout) itemView.findViewById(R.id.user_cjwt_rl);
            dzglRl = (RelativeLayout) itemView.findViewById(R.id.user_dzgl);
            wdscRl = (RelativeLayout) itemView.findViewById(R.id.user_wdsc_rl);
            wdfpRl = (RelativeLayout) itemView.findViewById(R.id.user_wdfp_rl);
            dzsmkRl = (RelativeLayout) itemView.findViewById(R.id.user_dzsmk_rl);
            realRl = (RelativeLayout) itemView.findViewById(R.id.user_real_ll);
            wdjkRl = (RelativeLayout) itemView.findViewById(R.id.user_wdjk_rl);
            gjjRl = (RelativeLayout) itemView.findViewById(R.id.user_gjj_rl);


        }
    }

}
